package cn.cinz.controller;


import cn.cinz.pojo.Blog;
import cn.cinz.service.BlogService;
import cn.cinz.util.IdUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 博客数据显示
 */

@RestController
@CrossOrigin
@RequestMapping("/user")
public class BlogJsonController {

    private static final Log logger = LogFactory.getLog(BlogJsonController.class);

    @Autowired
    @Qualifier("BlogServiceImpl")
    BlogService blogService;

    String msg = "";

    //显示自己的全部博客
    @RequestMapping("/queryBlogList")
    public String queryDiaryList(HttpSession session) {
        String userName = String.valueOf(session.getAttribute("userName"));
        List<Blog> blogList = blogService.queryBlogList(userName);//查找数据

        if (blogList != null) {
            Map<String, Object> map = new LinkedHashMap<>();//设置指定json结果集  有序
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", 1000);
            map.put("data", blogList);

            return JSON.toJSONString(map);

        } else {
            Blog blog = new Blog("null", new Date(), "null", "null", "null", "null");
            List<Blog> blogListError = new ArrayList<>();
            blogListError.add(blog);
            Map<String, Object> map = new LinkedHashMap<>();//设置指定json结果集  有序
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", 1000);
            map.put("data", blogListError);

            return JSON.toJSONString(map);
        }
    }

    //修改一篇博客
    @RequestMapping("/updateBlog")
    public String updateBlog(@Param("blogId") String blogId, @Param("blogTitle") String blogTitle,@Param("blogDate") Date blogDate, @Param("blogMemo") String blogMemo, @Param("blogStatus") String blogStatus, @Param("blogValue") String blogValue, HttpSession session){
        /**
         * 修改一篇博客 根据博客id 和 用户名判断是修改的哪条数据
         */
        Blog blog = new Blog(blogId,blogTitle,blogDate,blogMemo,blogValue,blogStatus,String.valueOf(session.getAttribute("userName")));
        int i = blogService.updateBlog(blog);
        if (i > 0){
            msg = "修改成功";
            logger.info("修改成功");
        }else {
            msg = "修改失败";
        }
        return msg;
    }


    //删除一篇博客
    @RequestMapping("/deleteBlog")
    public String deleteBlog(@Param("blogId") String blogId) {
        System.out.println("blogId:" + blogId);

        int i = blogService.deleteBlog(blogId);//删除博客方法调用
        if (i > 0) {
            msg = "删除成功";
        } else {
            msg = "删除失败";
        }


        return msg;
    }


    /**
     * 问题： 返回的json数据  根据blogTitle查询的列表 无法正常注入到列表里
     * @param blogTitle
     * @return
     */
    //显示所有用户公开博客
    @RequestMapping("/queryBlogStatusList")
    public String queryBlogStatusList(@Param("blogTitle") String blogTitle) {

        String blogStatus = "公开";
        List<Blog> blogStatusList = blogService.queryBlogStatusList(blogStatus,blogTitle);

        if (blogStatusList != null) {
            return JSON.toJSONString(blogStatusList);
        } else {
            return "没有您要找的数据";
        }
    }


}
